package name.thivent.sandbox.samples.services.hello;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import junit.framework.TestCase;

/**
 * Functional test of the EchoBean.
 * 
 * @version $Id$
 */
public class EchoTest extends TestCase {
    /**
     * Test the echo method of the EJB.
     */
    public final void testEcho() {
        assertEquals("Echo Echo", lookupEchoForJBoss().echo("Echo"));
    }

    /**
     * Lookup the remote interface on JBoss.
     * 
     * @return the Echo remote interface
     */
    private Echo lookupEchoForJBoss() {
        try {
            // In JBoss, one would have to use the name of the bean + "/remote"
            Context c = new InitialContext();
            return (Echo) c.lookup("EchoBean/remote"); // use myEarName/HelloWorldBean/remote in an ear
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    // CHECKSTYLE:OFF
    /**
     * Lookup the remote interface on GlassFish. This method is just there for documentation purpose and is currently
     * not used.
     * 
     * @return the echo remote interface
     */
    private Echo lookupEchoForGlassFish() {
        try {
            // In GlassFish (v1 and v2), one would have to use the FQN of the remote interface:
            Context c = new InitialContext();
            return (Echo) c.lookup(Echo.class.getName());
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    // CHECKSTYLE:ON
}
